<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Posted Properties</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .property {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
            overflow: hidden;
            display: flex;
            align-items: flex-start;
            width: 100%;
            height: 320px;
        }
        .property img {
            width: 480px;
            height: auto;
            border-right: 1px solid #ccc;
            margin-top: 20px;
            margin-left: 20px;
            border-radius: 10px;
        }
        .property-details {
            padding: 15px;
            flex: 1;
            margin-left: 30px;
        }
        .property-details h2 {
            margin-top: 0;
            color: #333;
        }
        .property-details p {
            margin: 5px 0;
            color: #666;
        }
        .property-actions {
            display: flex;
            justify-content: flex-end;
            margin-top: 10px;
        }
        .property-actions button {
            margin-left: 10px;
            padding: 8px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .delete-btn {
            background-color: #e74c3c;
            color: #fff;
            position: relative;
            top: -115px;
            right: 100px;
            width: 150px;
            height: 43px;
            font-size: 20px;
            letter-spacing: 4px;
        }
        .toggle-btn {
            background-color: #3498db;
            color: #fff;
            position: relative;
            top: -115px;
            right: 100px;
            width: 150px;
            height: 43px;
            font-size: 20px;
            letter-spacing: 4px;
        }
        .indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 5px;
        }
        .on-sale {
            background-color: green;
        }
        .off-sale {
            background-color: red;
        }
    </style>
</head>
<body>
<h1>我发布的房子</h1>
<div id="properties"></div>

<script>
    const token = localStorage.getItem('token');

    document.addEventListener('DOMContentLoaded', function() {
        const userId = localStorage.getItem('userId');
        if (!userId) {
            alert('User ID not found in localStorage');
            return;
        }

        fetch(`http://localhost:8080/user/getMyPostProperty?userId=${userId}`,
            {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    displayProperties(data.data);
                } else {
                    alert('Failed to fetch properties');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('Error fetching properties');
            });

        function createElement(tag, className, content) {
            const element = document.createElement(tag);
            if (className) element.className = className;
            if (content) element.textContent = content;
            return element;
        }

        function displayProperties(properties) {
            const propertiesContainer = document.getElementById('properties');
            propertiesContainer.innerHTML = ''; // Clear previous content to prevent duplication
            properties.forEach(property => {
                const propertyDiv = createElement('div', 'property');

                const image = createElement('img');
                image.src = property.imageUrl;
                propertyDiv.appendChild(image);

                const detailsDiv = createElement('div', 'property-details');

                detailsDiv.appendChild(createElement('h2', null, property.title));
                detailsDiv.appendChild(createElement('p', null, property.description));
                detailsDiv.appendChild(createElement('p', null, `位置: ${property.province} ${property.city} ${property.community}`));
                detailsDiv.appendChild(createElement('p', null, `房型: ${property.houseType}`));
                detailsDiv.appendChild(createElement('p', null, `面积: ${property.area} 平方米`));
                detailsDiv.appendChild(createElement('p', null, `价格: ¥${property.price.toLocaleString()}`));
                detailsDiv.appendChild(createElement('p', null, `类型: ${property.type === 0 ? '二手房' : '新房'}`));

                const statusDiv = createElement('div');
                statusDiv.appendChild(createElement('span', null, '状态：'));
                const statusIndicator = createElement('div', `indicator ${property.status === 1 ? 'on-sale' : 'off-sale'}`);
                statusDiv.appendChild(statusIndicator);
                statusDiv.appendChild(createElement('span', null, property.status === 1 ? '在售' : '停售'));
                detailsDiv.appendChild(statusDiv);

                const actionsDiv = createElement('div', 'property-actions');
                const deleteButton = createElement('button', 'delete-btn', '删除');
                deleteButton.onclick = function() {
                    alert(`删除房子ID: ${property.propertyId}`);
                };
                actionsDiv.appendChild(deleteButton);

                const toggleStatusButton = createElement('button', 'toggle-btn', property.status === 1 ? '停售' : '起售');
                toggleStatusButton.onclick = function() {
                    toggleStatus(property.propertyId, property.status === 1 ? 0 : 1);
                };
                actionsDiv.appendChild(toggleStatusButton);

                detailsDiv.appendChild(actionsDiv);
                propertyDiv.appendChild(detailsDiv);
                propertiesContainer.appendChild(propertyDiv);
            });
        }

        function toggleStatus(propertyId, newStatus) {
            fetch(`http://localhost:8080/user/updateMyPostProperty?propertyId=${propertyId}&status=${newStatus}`, {
                method: 'PUT',
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 1) {
                        alert("修改成功")
                        location.reload(); // Refresh the page to reflect the updated status
                    } else {
                        alert('Failed to update property status');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('Error updating property status');
                });
        }
    });
</script>
</body>
</html>
